log using ces_replication.log, replace


* Import data

use CCES21_TUF_OUTPUT.dta, clear

* Recode local items to that don't know is missing and no is 0 and to adjust for reverse coded items
recode TUF301a 1=0 2=1 3=., gen(taxbreaks_businesses)
recode TUF301b 1=0 2=1 3=., gen(increase_parking)
recode TUF301c 2=0 3=., gen(affordable_housing)
recode TUF301d 2=0 3=., gen(condemn_blight)
recode TUF301e 2=0 3=., gen(apt_buildings)
recode TUF301f 2=0 3=., gen(rent_controls)

* Comparing separately scaled national and local policy indexes

irt 2pl taxbreaks_businesses-rent_controls, intm(mc)
predict localscale, latent
irtgraph iif

* Recode national items and scale
recode CC21_320a-CC21_321f CC21_322a-CC21_324d CC21_350a-CC21_350i CC21_355a-CC21_355e (2=0)

irt 2pl CC21_320a-CC21_321f CC21_322a-CC21_324d CC21_350a-CC21_350i CC21_355a-CC21_355e, intm(mc)
predict natlscale, latent

* Reverse polarity to match polarity from Study 1
gen natlscale_lc=natlscale*-1


recode ideo5 6=3

pwcorr localscale natlscale ideo5, sig

recode urbancity 3/5=3, gen(urbancity2)
bysort urbancity2: pwcorr localscale natlscale_lc, sig


* Figure 5: Scatter plot of IRT issue scales (not in PAP)

twoway scatter localscale natlscale_lc, mc(black%50) aspect(1) xtitle("National issues scale") ytitle("Local issues scale") || lfit localscale natlscale_lc, lc(red) lp(solid) legend(off) text(0 2.5 "r = 0.575")
graph export Figure5.png, replace

* Figure 6: Local item correlations with national policy scale (Not in PAP)

local varlist  affordable_housing taxbreaks_businesses increase_parking condemn_blight apt_buildings rent_controls


local nvars : word count `varlist' 

local N = `nvars' * (`nvars' - 1) / 2 

if `N' > _N set obs `N' 

gen x2 = "" 
gen r2 = . 
local k = 1 
tokenize "`varlist'" 

forval i = 1/`nvars' { 
    local J = `i' + 1 
        quietly {
            corr ``i'' natlscale 
            replace x2 = "``i''" in `k' 
            replace r2 = r(rho) in `k' 
        }
        local ++k 
    }

gsort r2
gen variable=_n if r2!=.

label define issues 1 "Oppose - Increase parking" 2 "Oppose - Tax breaks for businesses" 3 "Support - Condemn blighted property" 4 "Support - Allow apartment buildings in neighborhood" 5 "Support - Rent control" 6 "Support - Affordable housing"
label values variable issues

twoway bar r2 variable if r2!=., hor ylabel(1(1)6, val) color(navy%70) ytitle(" ") xtitle("Correlation with national policy scale") xlabel(-.2(.1).6) aspect(1) barw(.75)
graph export Figure6.png, replace

* Robustness check for SI (Figure A4): Create a loop to randomly select 6 national policy items at a time for scaling 
* Do this 30 times
forval j = 1/30{

local setvars CC21_320a CC21_320b CC21_320c CC21_320d CC21_320e CC21_321a CC21_321b CC21_321c CC21_321d CC21_321e CC21_321f CC21_322a CC21_322b CC21_322c CC21_322d CC21_322e CC21_323a CC21_323b CC21_323c CC21_323d CC21_323e CC21_323f CC21_324a CC21_324b CC21_324c CC21_324d CC21_350a CC21_350b CC21_350c CC21_350d CC21_350e CC21_350f CC21_350g CC21_350h CC21_350i CC21_355a CC21_355b CC21_355c CC21_355d CC21_355e 

local size : word count `setvars'

set seed 123`j'


forval i = 1/6 {
   local nextvar = word("`setvars'", runiformint(1, `size'))
   di "newvar`i' will get the values of `nextvar'."
   gen newvar`i' = `nextvar'
   // Don't pick this var again
   local setvars: list setvars - nextvar
   local size = `size' - 1
}
irt 2pl newvar1 newvar2 newvar3 newvar4 newvar5 newvar6, intm(mc)
predict natscale`j'
drop newvar1 newvar2 newvar3 newvar4 newvar5 newvar6
}

* Local scale correlations with random national policy scales 

local varlist localscale natscale1 natscale2 natscale3 natscale4 natscale5 natscale6 natscale7 natscale8 natscale9 natscale10 natscale11 natscale12 natscale13 natscale14 natscale15 natscale16 natscale17 natscale18 natscale19 natscale20 natscale21 natscale22 natscale23 natscale24 natscale25 natscale26 natscale27 natscale28 natscale29 natscale30

local nvars : word count `varlist' 

local N = `nvars' * (`nvars' - 1) / 2 

if `N' > _N set obs `N' 

gen x = "" 
gen y = "" 
gen r = . 
local k = 1 
tokenize "`varlist'" 

forval i = 1/`nvars' { 
    local J = `i' + 1 
    forval j = `J'/`nvars' { 
        quietly {
            corr ``i'' ``j'' 
            replace x = "``i''" in `k' 
            replace y = "``j''" in `k' 
            replace r = r(rho) in `k' 
        }
        local ++k 
    }
}

* Make histogram showing distribution of correlations 
replace r = abs(r)

twoway histogram r if x!="localscale", color(black%30) percent || histogram r if x=="localscale", color(navy%60) percent bin(10) legend(order(2 "National scale vs." "national scale" 1 "Local scale vs." "national scale") pos(6) row(1)) xtitle("Absolute value of correlation coefficients") xlabel(0 .2 .4 .6 .8 1) aspect(1)
graph export FigureA4.png, replace

log close
